// pages/main/addWorkNotePage/addWorkNotePage.js
const app=getApp()
import util from '../../utils/utils';
import URL from '../../utils/urls';
import WxValidate from "../../utils/WxValidate";
let projectArrays;
let  project;
Page({
  


  /**
   * 页面的初始数据
   */
  data: {

      
      index: -1,
      projectNames:[],
      number: 1,
      disabled1: false,
      workNumber:8,
      overtimeNumber:0,
      projectName:'',
     
 
   

     

  
  },

  projectPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    project= projectArrays[e.detail.value]
    this.setData({
      
      index: e.detail.value,
      projectName:  this.data.projectNames[e.detail.value],
     
    })
     
  },
  workDateBindChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      workDate: e.detail.value
    })
  },


  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let params = e.detail.value;
 //校验表单
    if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
    
        //通过校验， 提交数据、
        this.saveWorkNote(app.globalData.token,e.detail.value)
       

 
  },
  

  //新增工作日志
  saveWorkNote:function(token,jstr){
    console.log("待上传的json值"+jstr)
    console.log("转换后的json值"+JSON.stringify(jstr))


    var workContentJstr={
      "content": jstr.workDetail,
      "difficult": jstr.workTrouble,
      "fillInDate": jstr.workDate,
      "id": 0,
      "normalTime": jstr.workNumber,
      "overTime": jstr.overtimeNumber,
      "personCode": app.globalData.user.personCode,
      "projectId": project.id,
      "remarks": jstr.workRemark
    };
    console.log(workContentJstr)
    wx.request({
      url: URL.URL_ADD_NEW_WORK_NOTE,
      method:'POST',
      header: {
        'Content-Type': 'application/json',
        'token':token
      },
      data: workContentJstr,
      success: (res) =>{
        console.log(res)


       if(res.data.status=="6000"){

        wx.showToast({
          title:"保存成功",
          icon:'none',
          duration:1500
        })

        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })  
        },1500)

       }else{
          console.log("新增工作日志失败") 

         wx.showToast({
           title:"新增工作日志失败",
           icon:'none',
           duration:3000
         })


       }
     },
     fail(res){
       console.log("新增工作日志失败") 
      wx.showToast({
        title:"新增工作日志失败",
        icon:'none',
        duration:3000
      })
       console.log(res)
     }
    })

  },


  
  prevNum(){
    this.setData({ number: this.data.number + 1 });
  },
  nextNum(){
    this.setData({ number: this.data.number - 1 });
  },




  //报错
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },


  //表单验证初始化方法
  initValidate() {
    let rules = {
      projectName: {
        required: true,
        
      },
      
      workNumber: {
        required: true,
        number: true,
       
      }, 
 
    }

    let message = {
      projectName: {
        required: '项目名称不能为空', 
      },
      workNumber: {
        required: "工作时长必须为数字"
      },
      
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();


    console.log(app.globalData.token)
    //查询所有项目数据
    wx.request({
      url: URL.URL_QUERY_ALL_PROJECT_INFO,
      method:'GET',
      header: {
        'Content-Type': 'application/json',
        'token':app.globalData.token
      },
      data:{
         
      },
      success: (res) =>{


       if(res.data.status=="3000"){
        console.log(res)
        projectArrays=res.data.data
        var projectNameArray=new Array();
        projectArrays.forEach(element => {
          projectNameArray.push(element.projectName)
        });
        this.setData({
          projectNames:projectNameArray,
         })
       

        console.log(projectArrays)
       }else{
        console.log(res)
        console.log("查询项目信息失败") 

         wx.showToast({
           title:"查询项目信息失败",
           icon:'none',
           duration:3000
         })
       }
     },
     fail(res){
       console.log("查询项目信息失败") 
        wx.showToast({
         title:'查询项目信息失败',
         icon:'none',
         duration:3000
       })
       console.log(res)
     }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})